package com.lx.pc.restful.supplydemand;

import java.io.IOException;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.lx.pc.domain.vo.category.CategoryVo;
import com.lx.pc.domain.vo.supplydemand.SupplydemandVo;
import com.lx.pc.domain.vo.supplydemandimg.SupplydemandimgVo;
import com.lx.pc.domain.vo.sysarea.SysareaVo;
import com.lx.pc.service.category.ICategoryService;
import com.lx.pc.service.supplydemand.ISupplydemandService;
import com.lx.pc.service.supplydemandimg.ISupplydemandimgService;
import com.lx.pc.service.sysarea.ISysareaService;
import com.lx.pc.utils.DateUtil;
import com.lx.pc.utils.OmsPage;

@Controller
@RequestMapping(name = "supplydemand")
public class SupplydemandController {

	@Resource(name = "supplydemandService")
	private ISupplydemandService  supplydemandService;
	
	@Resource(name ="supplydemandimgService")
	private ISupplydemandimgService supplydemandimgService;
	
	/**
	 * 地区
	 */
	@Resource(name = "sysareaService")
	private ISysareaService sysareaService;

	/**
	 * 类目
	 */
	@Resource(name = "categoryService")
	private ICategoryService categoryService;
	
	@RequestMapping(value = "/toSupplydemandList")
	public ModelAndView toSupplydemandList() {
		
		ModelAndView mv = new ModelAndView("/supplydemand/supply_demand_list");
		// 类目
		CategoryVo categoryVo = new CategoryVo();
		categoryVo.setType(1);
		categoryVo.setActive((short) 1);
		categoryVo.setStatus(1);
		List<CategoryVo> categoryList = categoryService.findByCategoryList(categoryVo);

		// 查询地区
		SysareaVo sysarea = new SysareaVo();
		sysarea.setParentCode("0");
		List<SysareaVo> sysareaList = sysareaService.findBySysareaList(sysarea);

		mv.addObject("sysarea", sysareaList);
		mv.addObject("category", categoryList);
		
		return mv;
	}
	
	@ResponseBody
	@RequestMapping("/supplydemandList")
    public  String supplydemandList(SupplydemandVo v,Integer page,Integer size) throws IOException {
        Map<String,Object> result = new HashMap<String, Object>();
        OmsPage<SupplydemandVo> pages = supplydemandService.findBySupplydemand(v, page, size);
        for (SupplydemandVo supplydemandVo : pages.getResult()) {
			//有效期
        	supplydemandVo.setDeadline(DateUtil.getContrastDayTime(supplydemandVo.getDeadline()));
		}
        result.put("supplydemandVo", pages);
        return JSON.toJSONString(result);
    }
	
	
	@ResponseBody
	@RequestMapping("/supplyDemanDetail")
	public ModelAndView findBySupplydemandId(String id){
		ModelAndView mv = new ModelAndView("/supplydemand/supply_demand_details");
		BigInteger bId = new BigInteger(id); 
		SupplydemandVo supplydemandVo = supplydemandService.findBySupplydemandId(bId);
		supplydemandVo.setDeadline(DateUtil.getContrastDayTime(supplydemandVo.getDeadline()));
		SupplydemandimgVo sVO = new SupplydemandimgVo();
		sVO.setSupplyDemandId(bId.toString());
		List<SupplydemandimgVo> SupplydemandimgVo = supplydemandimgService.findBySupplydemandimg(sVO);
		mv.addObject("supplydemandVo",supplydemandVo);
		mv.addObject("SupplydemandimgVo", SupplydemandimgVo);
		return mv;
		
		
	}
	
		
	
}
